#include "DataWriter.h"

/* file output */
#include <fstream>

/* strings and buffers */
#include <string>
#include <sstream>

#include <cv.h>

#include "../utils/Output.h"
//#include "../utils/Settings.h"
#include "../Features.h"


DataWriter::DataWriter()
{
}

DataWriter::~DataWriter()
{
}


void DataWriter::writeToFile(std::deque<std::string> fileNames, std::deque<Features::FeaturePair> featurePairs, std::deque<CvSize> imageSizes){
	//if (Settings::outputFileFormat == PTO){
		writeAsPTO(fileNames, featurePairs, imageSizes);
	/*} else {
		writeAsPADB(fileNames, featurePairs, imageSizes); //NOT IMPLEMETED YET!
	}*/
}
	

void DataWriter::writeAsPTO(std::deque<std::string> fileNames, std::deque<Features::FeaturePair> featurePairs, std::deque<CvSize> imageSizes){
	using std::string;
	using std::ofstream;
	using std::ostringstream;
	using std::ios_base; //that's a whole namespace
	using std::endl;
	
	
	//ofstream dataFile("PTO/input.pto", ios_base::trunc|ios_base::out);
	ofstream dataFile("input_filtered.pto", ios_base::trunc|ios_base::out);

	if(!dataFile.is_open()){
		Output::message("Could not open data file for output!", Output::CRITICAL);
	} else {
		
		Features::FeaturePair currentPair;
		
		//ostringstream line;
		
		string imageParameters;
		
		//char pathDelimiter = '/';
		
		
		dataFile << "# MaKI TIE-POINT project file" << endl;
		dataFile << "#hugin_ptoversion 2" << endl;
		dataFile << endl;
		
		imageParameters = "f0 Eb1 Eev0 Er1 Ra0 Rb0 Rc0 Rd0 Re0 Va1 Vb0 Vc0 Vd0 Vx0 Vy0 a0 b0 c0 d0 e0 g0 p0 r0 t0 v21.6437508013246 y0  Vm5 u10 n";
		
		dataFile << "# image lines" << endl;
		
		for (unsigned int i = 0; i < fileNames.size(); ++i) {
			dataFile << "#-hugin  cropFactor=1" << endl;
			dataFile << "i ";
			dataFile << "w" << imageSizes[i].width << " ";
			dataFile << "h" << imageSizes[i].height << " ";
			dataFile << imageParameters; //No endl!
			dataFile << "\"" << fileNames[i] << "\"" << endl; ///TODO check for relative vs. absolute paths!
		}
		dataFile << endl;
		dataFile << endl;

		dataFile << "# control points" << endl;
		//for (unsigned int i = 0; i < featurePairs.size(); ++i) {
			for (unsigned int i = 0; i < featurePairs.size(); ++i) {
				//currentPair = (Features::FeaturePair*)cvGetSeqElem(featurePairs[i], j);
				currentPair = featurePairs[i];
				dataFile << "c ";
				dataFile << "n" << currentPair.leftImageID << " ";
				dataFile << "N" << currentPair.rightImageID << " ";
				dataFile << "x" << currentPair.left.x << " ";
				dataFile << "y" << currentPair.left.y << " ";
				dataFile << "X" << currentPair.right.x << " ";
				dataFile << "Y" << currentPair.right.y << " ";
				dataFile << "t" << currentPair.id << " ";
				
				dataFile << endl;
			}
		//}
		dataFile << endl;
		
		
		
		dataFile.close();
	}
	
	
}



/*void DataWriter::writeAsPADB(std::vector<std::string> fileNames, std::deque<CvSeq*> featurePairs, std::deque<CvSize> imageSizes){
	///TODO implement
	Output::message("Output to PADB file format is not yet supported", Output::CRITICAL);
}*/
